<?php


namespace app\api\controller;
use GTClient;
use GTPushRequest;
use GTPushMessage;
use GTNotification;
use GTSettings;
use GTPushChannel;
use GTAndroid;
use GTIos;
use GTAps;
use GTAlert;
use GTUps;
use GTThirdNotification;
use think\Controller;

class Unipush extends Controller
{
    private $APPKEY = 'Ik9Kj6QzWP6mKnh7kCT6e5';
    private $APPID = 'HG6M4ASvHBAgCOlkZpiqiA';
    private $MASTERSECRET = 'e01IZQtw5nANMA3rcPjwe5';

    public function index(){
        $time = date ( "m-d H:i:s" ,time());
        $title = $time."系统下发通知";
        $content = '系统下发通知,支持在线、离线接收';
        $cid = '40c37069616cbeff430c9129b9c7a7f9';
        $package = 'uni.UNI09AB0C6';
        $payload = '自定义消息';

        $result = $this->pushToSingleByCid($title,$content,$payload,$cid,$package);
        dump($result);
    }

    //推送API_根据cid进行单推
    function pushToSingleByCid($title,$content,$payload,$cid,$package){

        //创建API，APPID等配置参考 环境要求 进行获取
        $api = new GTClient("https://restapi.getui.com",$this->APPKEY, $this->APPID,$this->MASTERSECRET);
        //设置推送参数
        if(is_array($payload)){
            $pj =  json_encode($payload);
        }
        else{
            $pj = $payload;
        }

        $push = new GTPushRequest();
        $osn = md5(time() . mt_rand(1, 9999999));

        $push->setRequestId((string)$osn);
        $message = new GTPushMessage();
        $intent = "intent:#Intent;launchFlags=0x14000000;action=android.intent.action.oppopush;component={$package}/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title={$title};S.content={$content};S.payload={$pj};end";

        //个推
        $notify = new GTNotification();
        $notify->setTitle($title);
        $notify->setBody($content);
        $notify->setPayload($pj);
        $notify->setBadgeAddNum(1);
        $notify->setClickType("intent");
        $notify->setIntent($intent);
        $message->setNotification($notify);

        $channel = new GTPushChannel();
        //Android
        $thirdnotify = new GTThirdNotification();
        $ups = new GTUps();
        $gtAndroid = new GTAndroid();
        $thirdnotify->setTitle($title);
        $thirdnotify->setBody($content);
        $thirdnotify->setPayload($pj);
        $thirdnotify->setClickType("intent");
        $thirdnotify->setIntent($intent);
        $ups->setNotification($thirdnotify);
        // $upsback= $ups->setTransmission(json_encode($touchuan));//厂商透传
        $gtAndroid->setUps($ups);
        $channel->setAndroid($gtAndroid);
        //echo  $intent;
        //点击通知后续动作，目前支持以下后续动作:
        //1、intent：打开应用内特定页面url：打开网页地址。2、payload：自定义消息内容启动应用。3、payload_custom：自定义消息内容不启动应用。4、startapp：打开应用首页。5、none：纯通知，无后续动作
        //$notify->setIntent($intent);
        // $notify->setChannelLevel(3);
        // $touchuan=['title'=>$title,'content'=>$content,'payload'=>$package];

//$message->setTransmission(json_encode($touchuan));//个推透传

        //ios
        $ios = new GTIos();
        $ios->setType("notify");
        $ios->setAutoBadge("1");

        $ios->setPayload($pj);

        //aps设置
        $aps = new GTAps();
        $aps->setContentAvailable(0);


        $alert = new GTAlert();
        $alert->setTitle($title);
        $alert->setBody($content);

        $aps->setAlert($alert);
        $ios->setAps($aps);
        $channel->setIos($ios);
        $push->setPushMessage($message);
        $push->setPushChannel($channel);
        $push->setCid($cid);
        //处理返回结果
        $result = $api->pushApi()->pushToSingleByCid($push);
        // print_r($result);
        return $result;

    }


    //统计API_获取单日推送数据
    function queryPushResultByDate(){
        $appKey = 'Ik9Kj6QzWP6mKnh7kCT6e5';
        $appId = 'HG6M4ASvHBAgCOlkZpiqiA';
        $masterSecret = 'e01IZQtw5nANMA3rcPjwe5';
        //创建API，APPID等配置参考 环境要求 进行获取
        $api = new GTClient("https://restapi.getui.com",$appKey, $appId,$masterSecret);
        //处理返回结果
        $result = $api->statisticsApi()->queryPushResultByDate("2022-5-10");
        dump($result);
    }
}